<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>网络唤醒工具 V1.0</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link href="bootstrap.min.css" rel="stylesheet">
    <style>
        body {
            width: 100%;
            height: 100%;
            background: #e9ecef;
        }
    </style>
</head>
<body>
<div class="container">
    <div class="jumbotron">
        <h1 class="display-3 text-center">网络唤醒工具 V1.0</h1>
        <p class="lead text-center">在下方填写你的IP地址和MAC地址即可远程唤醒</p>
    </div>
</div>
<div class="container d-flex justify-content-center align-items-center">
    <div class="row">
        <form class="mt-5">
            <div class="row">
                <div class="col">
                    <input id="ip" type="text" class="form-control" placeholder="IP地址">
                </div>
                <div class="col">
                    <input id="mac" type="text" class="form-control" placeholder="Mac地址">
                </div>
            </div>
            <div class="form-group row mt-4">
                <div class="col">
                    <button type="button" id="btn" class="btn btn-primary btn-block">唤醒</button>
                </div>
            </div>
        </form>
    </div>

</div>
</body>
<script src="jquery.min.js"></script>
<script src="sweetalert2.js"></script>
<script>

    function isValidIP(ip) {
        var reg = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/
        return reg.test(ip);
    }

    function checkMacAddress(macAddress) {
        // var regex = "([A-Fa-f0-9]{2}-){5}[A-Fa-f0-9]{2}";
        var regex = "(([A-Fa-f0-9]{2}-){5}[A-Fa-f0-9]{2})|(([A-Fa-f0-9]{2}:){5}[A-Fa-f0-9]{2})"; // 含冒号
        var regexp = new RegExp(regex);
        if (!regexp.test(macAddress)) {
            return false;
        }
        return true;
    }

    $(function () {
        $("#btn").click(function () {
            if (!isValidIP($("#ip").val())) {
                Swal.fire({
                    icon: 'error',
                    title: 'Oops...',
                    text: 'IP地址填写不正确',
                })
                return;
            }
            if (!checkMacAddress($("#mac").val())) {
                Swal.fire({
                    icon: 'error',
                    title: 'Oops...',
                    text: 'MAC地址填写不正确',
                })
                return;
            }
            $.ajax({
                url: '/wakeup',
                type: 'get',
                data: {
                    ip: $("#ip").val(),
                    mac: $("#mac").val(),
                },
                dataType:'json',
                success: function (res) {
                    if (res.Code == 0) {
                        Swal.fire({
                            text: '发送唤醒成功,请等待30秒左右主机上线~',
                            imageUrl: '/tips.jpg',
                            imageWidth: 300,
                            imageHeight: 214,
                            imageAlt: '睡你麻痹起来嗨',
                        })
                    } else {
                        swal.fire('操作失败', res.msg, 'error');
                    }
                }
            });
        });
    });
</script>
</html>